package com.example.icongyou.retrofit;

import static android.content.Context.MODE_PRIVATE;

import android.content.SharedPreferences;

import com.example.icongyou.MyApplication;
import com.example.icongyou.api.ApiService;
import com.example.icongyou.api.AuthApi;
import com.example.icongyou.api.ChapterApi;
import com.example.icongyou.api.ChapterResourceApi;
import com.example.icongyou.api.CourseApi;
import com.example.icongyou.api.PersonalDashboardApi;
import com.example.icongyou.api.StudentApi;
import com.example.icongyou.api.StudentCourseApi;
import com.example.icongyou.api.TeacherApi;
import com.example.icongyou.api.TeacherCourseApi;
import com.example.icongyou.api.TaskDashboardApi;
import com.example.icongyou.api.TeamApi;
import com.example.icongyou.api.TeamAssignmentApi;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

// RetrofitClient.java - 网络请求客户端
public class RetrofitClient {
    private static final String BASE_URL = "http://192.168.43.144:8080/";// 换成你自己本机的ip地址
    private static Retrofit retrofit = null;

    // 单例模式获取Retrofit实例
    public static Retrofit getInstance() {
        return getClient(); // 内部复用已有的getClient()逻辑
    }

    public static Retrofit getClient() {
        if (retrofit == null) {
            OkHttpClient okHttpClient = new OkHttpClient.Builder()
                    .addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
                    .connectTimeout(30, TimeUnit.SECONDS)
                    .readTimeout(30, TimeUnit.SECONDS)
                    .writeTimeout(30, TimeUnit.SECONDS)
                    .build();

            retrofit = new Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .client(okHttpClient)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        }
        return retrofit;
    }

    // 获取各API实例（统一使用getInstance()确保单例）
    public static TaskDashboardApi getTaskDashboardApi() {
        return getInstance().create(TaskDashboardApi.class);
    }

    // 用于cos传输的api配置
    public static ApiService getApiService() {
        return getInstance().create(ApiService.class);
    }

    public static TeacherApi getTeacherApi() {
        return getInstance().create(TeacherApi.class);
    }

    public static StudentApi getStudentApi() {
        return getInstance().create(StudentApi.class);
    }

    public static ChapterApi getChapterApi() {
        return getInstance().create(ChapterApi.class);
    }

    public static ChapterResourceApi getChapterResourceApi() {
        return getInstance().create(ChapterResourceApi.class);
    }

    public static CourseApi getCourseApi() {
        return getInstance().create(CourseApi.class);
    }

    public static StudentCourseApi getStudentCourseApi() {
        return getInstance().create(StudentCourseApi.class);
    }

    public static TeacherCourseApi getTeacherCourseApi() {
        return getInstance().create(TeacherCourseApi.class);
    }

    // 新增：获取个人看板API实例
    public static PersonalDashboardApi getPersonalDashboardApi() {
        return getInstance().create(PersonalDashboardApi.class);
    }

    // 团队相关API实例（保持与其他接口调用风格一致）
    public static TeamApi getTeamApi() {
        return getInstance().create(TeamApi.class);
    }

    public static TeamAssignmentApi getTeamAssignmentApi() {
        return getClient().create(TeamAssignmentApi.class);
    }
    public static AuthApi getAuthApi() {
        return getInstance().create(AuthApi.class);
    }

    private static OkHttpClient getOkHttpClient() {
        OkHttpClient.Builder builder = new OkHttpClient.Builder()
                .connectTimeout(15, TimeUnit.SECONDS)
                .readTimeout(15, TimeUnit.SECONDS)
                .writeTimeout(15, TimeUnit.SECONDS);

        // 添加认证拦截器
        builder.addInterceptor(chain -> {
            SharedPreferences sp = MyApplication.getContext().getSharedPreferences("user_info", MODE_PRIVATE);
            String token = sp.getString("token", "");

            Request original = chain.request();
            Request request = original.newBuilder()
                    .header("Authorization", "Bearer " + token)
                    .build();
            return chain.proceed(request);
        });

        return builder.build();
    }

}